<?php
class WysiwygHelper extends FormHelper {
	function render($field_name, $type = 'ckeditor', $options = array()) {
		$_defaults = array(
			'fullPage'	=>	false
		);
		foreach ($_defaults as $key => $value) {
			$options[$key] = $value;
		}
		if ($type == 'ckeditor') {
			$name_parts = explode('.', $field_name);
			if (count($name_parts) == 2)
			$content = "<script language='javascript'>
				CKEDITOR.replace( 'data[{$name_parts[0]}][{$name_parts[1]}]',
			    {";
			
			$cnt = 0;
			foreach ($options as $config => $value) {
				if ($cnt > 0) {
					$content .= ",\n";
				}
				if ($config != "toolbar" && !is_bool($value)) {
					$content .= "{$config} : '{$value}'";
				} else {
					if (is_bool($value)) {
						$content .= "{$config} : ".($value ? "true" : "false");
					} else {
						$content .= "{$config} : {$value}";
					}
				}
				$cnt++;
			}

			$content .= "    
			,
			filebrowserBrowseUrl : '".$this->url('/js/')."ckfinder/ckfinder.html'
			});
				</script>";
			return $content;
		}
	}
	
	
	function detach($field_name, $type = 'ckeditor') {
		$content = "var instance = CKEDITOR.instances['{$field_name}'];
			if (instance) {
				instance.destroy();
			}
		} else {
			for (var instanceName in CKEDITOR.instances) {
				CKEDITOR.instances[instanceName].destroy();
			}
		}";
		return $content;
	}
}
?>